<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

class PieceRateDetail extends BaseController
{
    protected $table='piece_rate_detail';
    protected $title='计件工资明细';
    private $sessionKey='pagePieceRateDetail';

    /**
     * @return \json|\think\response\View
     * @throws \think\db\exception\DbException
     * date:2024/7/9
     * 计件工资明细
     */
    public function index()
    {

        $param=$this->param;
        if (request()->isAjax()) {
            $where[]=['status','>',-1];
            if (isset($param['orderDate'])&&$param['orderDate']){
                $times=explode('~',$param['orderDate']);
                $where[]=['orderDate','between',$times];
            }
            foreach (['craftName','teamName','userName','circulationNum','bar_code','mdName','specs'] as $v){
                if (isset($param[$v])&&$param[$v]){
                    $where[]=[$v,'like','%'.$param[$v].'%'];
                }
            }
            foreach (['report_id','status','pricing_mode'] as $v){
                if (isset($param[$v])&&$param[$v]){
                    $where[]=[$v,'=',$param[$v]];
                }
            }

            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name('jijianmingxi_view')
                ->where($where)
                ->order('id DESC')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            return view();
        }

    }

    /**
     * @return \json
     * date:2024/7/10
     * 复核
     */
    public function doCheck()
    {
        $param=$this->param;
        $ids=$param['id']??'';
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id','in',$ids)->column('status');
        if (in_array('2',$status)){
            return to_assign(1,'请勿重复复核');
        }
        Db::StartTrans();
        try {
            $upData=[
                'fh_user'=>$this->uid,
                'update_time'=>time(),
                'status'=>2
            ];
            Db::name($this->table)->where('id','in',$ids)->update($upData);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        add_log('check', $ids, $this->param,$this->title);
        return to_assign();
    }

    /**
     * 撤销
     * @return \json
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/7/10
     */
    public function doCancel()
    {
        $param=$this->param;
        $ids=$param['id']??'';
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id','in',$ids)->column('status');
        if (in_array('1',$status)){
            return to_assign(1,'操作有误');
        }
        Db::StartTrans();
        try {
            $upData=[
                'fh_user'=>$this->uid,
                'update_time'=>time(),
                'status'=>1
            ];
            Db::name($this->table)->where('id','in',$ids)->update($upData);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        add_log('back', $ids, $this->param,$this->title);
        return to_assign();
    }

    public function exportData()
    {
        $where=session($this->sessionKey);
        $mStatus=['','草稿','已确认'];
        $list = Db::name('jijianmingxi_view')
            ->where($where)
            ->order('id DESC')
            ->select()
            ->each(function ($item) use ($mStatus){
                $item['status']=$mStatus[$item['status']];
                return $item;
            });
        $title = [
            'id' => 'ID',
            'orderDate' => '日期',
            'report_id' => '报工id',
            'circulationNum' => '流转id',
            'craftName' => '工序',
            'pricing_mode' => '计价方式',
            'teamName' => '班组',
            'userName' => '个人',
            'bar_code' => '产品编码',
            'mdName' => '产品名称',
            'specs' => '规格型号',
            'unit' => '单位',
            'qualifiedNum' => '合格数量',
            'price' => '计件单价',
            'salary' => '计件工资',
            'status' => '状态',
        ];
        $name =$this->title;
        exportXlsData($name,$title,$list);
        exit();
    }
}